//
//  LGTRWZOpetateRecodeCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/8/29.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

class LGTRWZOpetateRecodeCell: UITableViewCell {

    var topLine:UIImageView?
    var bottomLine:UIImageView?
    var stataImageView:UIImageView?
    var mainImageViews:UIImageView?
    var titleLabel:UILabel?
    var timeLabel:UILabel?
    var descLabel:UILabel?
    var imageArray:Array<UIImageView> = []
    private var _model:Dictionary<String, Any>?
    var model:Dictionary<String, Any>{
        get{
            return _model ?? Dictionary.init()
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        self.contentView.backgroundColor = ViewBackColor
        let stateImageView:UIImageView = LGTViewCreateUtil.share.createImageView(image: "sign")
        self.addSubview(stateImageView)
        stateImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(SizeFitFloat(26))
            make.top.equalTo(SizeFitFloat(42))
            make.left.equalTo(SizeFitFloat(14))
        }
        
        self.topLine = UIImageView.init()
        topLine?.backgroundColor = YellowColor
        self.addSubview(self.topLine!)
        self.topLine!.snp.makeConstraints { (make) in
            make.width.equalTo(1)
            make.top.equalTo(0)
            make.bottom.equalTo(stateImageView.snp_topMargin).offset(-SizeFitFloat(6))
            make.centerX.equalTo(stateImageView)
        }
        
        self.bottomLine = UIImageView.init()
        bottomLine?.backgroundColor = YellowColor
        self.addSubview(self.bottomLine!)
        self.bottomLine!.snp.makeConstraints { (make) in
            make.width.equalTo(1)
            make.bottom.equalTo(0)
            make.top.equalTo(stateImageView.snp_bottomMargin).offset(SizeFitFloat(6))
            make.centerX.equalTo(stateImageView)
        }
        
        let arrowImageView:UIImageView = LGTViewCreateUtil.share.createImageView(image: "arrow")
        self.addSubview(arrowImageView)
        arrowImageView.snp.makeConstraints { (make) in
            make.width.height.equalTo(SizeFitFloat(20))
            make.left.equalTo(stateImageView.snp_rightMargin).offset(SizeFitFloat(15))
            make.centerY.equalTo(stateImageView)
        }
        
        let backView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(5))
        backView.backgroundColor = UIColor.white
        self.addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.top.equalTo(SizeFitFloat(20))
            make.left.equalTo(arrowImageView.snp_rightMargin)
            make.right.equalTo(SizeFitFloat(-12))
            make.bottom.equalTo(0)
        }
        
        self.titleLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(18))
        backView.addSubview(self.titleLabel!)
        self.titleLabel!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.height.equalTo(SizeFit(18))
            make.top.equalTo(SizeFit(17))
        }
        
        self.timeLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12))
        backView.addSubview(self.timeLabel!)
        self.timeLabel!.snp.makeConstraints { (make) in
            make.right.equalTo(SizeFit(-21))
            make.height.equalTo(SizeFit(18))
            make.top.equalTo(SizeFit(17))
        }
        
        self.descLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: GrayColor, fontSize: SizeFitFloat(12))
        self.descLabel?.numberOfLines = 0
        backView.addSubview(self.descLabel!)
        self.descLabel!.snp.makeConstraints { (make) in
            make.right.equalTo(SizeFit(-21))
            make.top.equalTo(SizeFit(45))
            make.left.equalTo(SizeFit(13))
        }
        
        self.mainImageViews = UIImageView.init()
        //self.mainImageViews?.backgroundColor = UIColor.orange
        backView.addSubview(self.mainImageViews!)
        self.mainImageViews!.snp.makeConstraints { (make) in
            make.left.equalTo(SizeFit(13))
            make.right.equalTo(-SizeFit(21))
            make.height.equalTo(0)
            make.top.equalTo(self.descLabel!.snp_bottomMargin).offset(SizeFitFloat(13))
            make.bottom.equalTo(-SizeFit(14))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setData(){
        self.titleLabel?.text = self.model["title"] as? String ?? ""
        self.timeLabel?.text = self.model["datetime"] as? String ?? ""
        self.descLabel?.text = self.model["message"] as? String ?? ""
        self.topLine?.isHidden = self.model["istop"] as? Bool ?? false
        self.bottomLine?.isHidden = self.model["isbottom"] as? Bool ?? false
        
        let imageArray:Array<Dictionary<String, Any>> = self.model["image"] as? Array<Dictionary<String, Any>> ?? []
        
        if imageArray.count == 0{
            self.mainImageViews?.isHidden = true
            self.mainImageViews!.snp.updateConstraints { (make) in
                make.height.equalTo(0)
            }
            return;
        }
        
        self.mainImageViews?.isHidden = false
        for i in 0..<self.imageArray.count{
            let imageView:UIImageView = self.imageArray[i]
            imageView.removeFromSuperview()
        }
        
        let w = SizeFitFloat(82)
        let margin = SizeFitFloat(12)
        let count = imageArray.count
        for i in 0..<count{
            let dict:Dictionary<String, Any> = imageArray[i]
            let url:String = dict["url"] as? String ?? ""
            if i > self.imageArray.count - 1{
                let imageView:UIImageView = LGTViewCreateUtil.share.createImageView(image: "")
                imageView.layer.masksToBounds = true
                self.imageArray.append(imageView)
            }
            
            let imageView:UIImageView = self.imageArray[i]
            imageView.backgroundColor = UIColor.white
            self.mainImageViews?.addSubview(imageView)
            imageView.snp.makeConstraints { (make) in
                make.top.equalTo((margin + w) * CGFloat(i / 3))
                make.left.equalTo((margin + w) * CGFloat(i % 3))
                make.width.height.equalTo(w)
            }
            
            BaseUtil.baseUtil.setImage(imageView: imageView, imageUrl: url)
        }
        
        let viewH = (margin + w) * CGFloat(count % 3 == 0 ? count/3 : count/3 + 1 ) - margin
        self.mainImageViews!.snp.updateConstraints { (make) in
            make.height.equalTo(viewH)
        }
    }
}
